import yaml
import os
from utils.handle_path import datas_path, configs_path


# 读取yaml文件
class YamlReader:
    # data_yaml = os.path.join(configs_path, 'loguru.yaml')
    data_yaml = os.path.join(datas_path, 'ElementsCenter.yml')

    def __init__(self, filename=data_yaml):
        try:
            with open(filename, 'r', encoding='utf-8') as file:
                # with open(os.path.dirname(os.path.dirname(__file__))+'/datas/ElementsCenter.yml','r',encoding='utf-8') as file:
                # self.data = yaml.load(file, Loader=yaml.FullLoader)
                self.data = yaml.safe_load(file)

        except:
            raise FileExistsError("文件没有找到")

    def get_data(self):
        return self.data

    def get_url(self, page, url_name):
        url = self.data[page][url_name]
        if url['url']:
            return url['url']
        """
        读取url
        :param page:
        :param url_name:
        :return:
        """
        # return self.datas[page][url_name]

    def get_element(self, page, element_name):
        element = self.data[page][element_name]
        if element['locater_by']:
            if element['locater_by'].lower() == 'xpath':
                by = 'xpath'
            elif element['locater_by'].lower() == 'id':
                by = 'id'
            elif element['locater_by'].lower() == 'link_text':
                by = 'link_text'
            elif element['locater_by'].lower() == 'partial_link_text':
                by = 'partial_link_text'
            elif element['locater_by'].lower() == 'tag_name':
                by = 'tag_name'
            elif element['locater_by'].lower() == 'class_name':
                by = 'class_name'
            elif element['locater_by'].lower() == 'css':
                by = 'css selector'
            else:
                raise Exception(f'定位方式不存在{element["locater_by"]}')
        else:
            raise Exception(f'定位方式不应该为空')

        value = element["value"]

        return [by, value]


if __name__ == '__main__':
    # print(YamlReader().get_element("LoginPage","username"))
    # print(YamlReader().get_url("PLS_Home", "url"))
    # print(YamlReader().get_element("LoginPage1","username"))
    print(YamlReader().get_data())
